# Library ----
library(readxl)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(leaflet)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(DT)
setwd("C:/Users/alfredo.acosta/OneDrive - SVA/LORA/Working example/ocurrence_of_disease/")
w <- read_xlsx("infur_20230407.xlsx")
setwd("C:/Users/alfredo.acosta/OneDrive - SVA/LORA/Working example/ocurrence_of_disease/")
wm <- st_read("TM_WORLD_BORDERS_SIMPL-0.3.shp")
## Reading layer `TM_WORLD_BORDERS_SIMPL-0.3' from data source
## `C:\Users\alfredo.acosta\OneDrive - SVA\LORA\Working example\ocurrence_of_disease\TM_WORLD_BORDERS_SIMPL-0.3.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 246 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -180 ymin: -90 xmax: 180 ymax: 83.57027
## Geodetic CRS: WGS 84
New dseases Contract 2
# table(w$disease_eng)
#Second contract
disease_to_analyse <- c("Venezuelan equine encephalomyelitis",
"Equine encephalomyelitis (Eastern)(2006-)",
"Equine encephalomyelitis (Western)(2006-)",
"Echinococcosis/hydatidosis",
"Leishmaniosis",
"Q fever")
# Dates
w$date <- floor_date(w$`event_start date`, unit="month")
# Define a list to store each map
map_list <- list()
# Iterate over each disease in disease_to_analyse
for (disease in disease_to_analyse) {
# Filter data for the specific disease
outb <- w %>%
filter(disease_eng == disease) %>%
group_by(disease_eng, Species_fg, iso_code, country, reporting_level) %>%
summarize(outbreak=n(), cases=sum(cases, na.rm = TRUE))
# Pop up for visualization with collapsed information just to see years and species showing the number of cases
outb_pop <- w %>%
filter(disease_eng == disease) %>%
group_by(disease_eng, iso_code, country) %>%
summarize(outbreak=n(),
species=paste(unique(Species_fg), collapse = ","),
years=paste(unique(year(date)), collapse = ","))
# Update the number of outbreaks and cases on the map
wm$outbreak <- outb$outbreak[match(wm$ISO3, outb$iso_code)]
wm$cases <- outb$cases[match(wm$ISO3, outb$iso_code)]
wm$years <- outb_pop$years[match(wm$ISO3, outb_pop$iso_code)]
wm$species <- outb_pop$species[match(wm$ISO3, outb_pop$iso_code)]
# Set color palette for outbreaks
pal <- colorNumeric(
palette = "Spectral",
domain = wm$cases,
na.color = "white")
# Create the map for the specific disease
map <- leaflet(wm) %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
setView(lng = 25, lat = 10, zoom = 2) %>%
addPolygons(weight = 0.5,
color = ~pal(cases),
popup = ~paste(NAME, years, species)) %>%
addLegend("bottomright", pal = pal, values = ~ outbreak,
title = paste("Outbreaks", "<br>", "<br>", disease),
opacity = 1)
# Store each map in the list with the disease name as the key
map_list[[disease]] <- map
}
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
## Warning in pal(c(r[1], cuts, r[2])): Some values were outside the color scale
## and will be treated as NA
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
## `summarise()` has grouped output by 'disease_eng', 'iso_code'. You can override
## using the `.groups` argument.
# Now map_list contains all maps for each disease
# Show maps
map_list[[1]]
map_list[[2]]
#There are Reported cases in Mexico
map_list[[3]]
map_list[[4]]
map_list[[5]]
map_list[[6]]
datatable(w %>%
filter(disease_eng == disease_to_analyse) %>%
group_by(disease_eng, Species_fg, iso_code, country, year(date)) %>%
summarize(outbreak=n(), cases=sum(cases, na.rm = TRUE))
)
## Warning: There was 1 warning in `filter()`.
## ℹ In argument: `disease_eng == disease_to_analyse`.
## Caused by warning in `disease_eng == disease_to_analyse`:
## ! longer object length is not a multiple of shorter object length
## `summarise()` has grouped output by 'disease_eng', 'Species_fg', 'iso_code',
## 'country'. You can override using the `.groups` argument.
Acosta, Alfredo PhD1. SVA1: SVA http://www.sva.se/.